package com.Logistics.pdusers.service.impl;

import com.Logistics.mycommon.pojo.pdusers.PdUser;
import com.Logistics.pdusers.mapper.PdUserMapper;
import com.Logistics.pdusers.service.PdUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.HashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fuqinggui
 * @since 2024-08-06
 */
@Service
public class PdUserServiceImpl extends ServiceImpl<PdUserMapper, PdUser> implements PdUserService {

    @Autowired
    private PdUserMapper pdUserMapper;


    @Override
    public PdUser Login(HashMap<String, String> map) {

        if(StringUtils.isNotBlank(map.get("userPhone")) && StringUtils.isNotBlank(map.get("passWord"))){
            QueryWrapper<PdUser> pdUserQueryWrapper = new QueryWrapper<>();
            pdUserQueryWrapper.eq("user_phone",map.get("userPhone")).eq("user_password", map.get("passWord"));
            PdUser pdUser = pdUserMapper.selectOne(pdUserQueryWrapper);
            if(pdUser!=null){
                return pdUser;
            }
        }
        return null;
    }
    @Override
    public  HashMap<String,String> saveUserInfo(HashMap<String,String> map) {
        Jedis jedis = new Jedis("192.168.137.148",6379);
        String msg = jedis.get("msg");
        jedis.close();
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        if(StringUtils.isNotBlank(map.get("yzm"))){
            if(map.get("yzm").equals(msg)){
                QueryWrapper<PdUser> pdUserQueryWrapper = new QueryWrapper<>();
                pdUserQueryWrapper.eq("user_phone",map.get("phoneNumber"));
                PdUser pdUser = pdUserMapper.selectOne(pdUserQueryWrapper);
                if(pdUser==null){
                    PdUser user = new PdUser();
                    user.setUserPhone(map.get("phoneNumber"));
                    user.setUserPassword(map.get("userPassword"));
                    int insert = pdUserMapper.insert(user);
                    if(insert>0){
                        stringStringHashMap.put("enroll","注册成功！");
                    }
                }else{
                    stringStringHashMap.put("UserStatus","该手机号已注册过！");
                }
            }else{
                stringStringHashMap.put("msgStatus","验证码错误或已过期！");
            }
        }

        return stringStringHashMap;
    }

    @Override
    public HashMap<String, Object> selectByMsg(HashMap<String, String> map) {
        Jedis jedis = new Jedis("192.168.137.148",6379);
        String msg = jedis.get("msg");
        jedis.close();

        HashMap<String, Object> hashMap = new HashMap<>();

        if(StringUtils.isNotBlank(String.valueOf(map.get("yzm")))){
            if(map.get("yzm").equals(msg)){
                QueryWrapper<PdUser> pdUserQueryWrapper = new QueryWrapper<>();
                pdUserQueryWrapper.eq("user_phone",map.get("phoneNumber"));
                PdUser pdUser = pdUserMapper.selectOne(pdUserQueryWrapper);
                if(pdUser==null){
                    hashMap.put("phoneStatus","该账号未注册!");
                }else{
                    hashMap.put("userInfo",pdUser);
                }
            }else{
                hashMap.put("msgStatus","验证码错误或已过期！");
            }
        }
        return hashMap;
    }
}
